package com.lagou.edu.service.impl;

import com.lagou.edu.dao.CodeDao;
import com.lagou.edu.pojo.LagouAutoCode;
import com.lagou.edu.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CodeServiceImpl implements ICodeService {

    @Autowired
    private CodeDao codeDao;

    @Override
    public LagouAutoCode saveCode(LagouAutoCode lagouAutoCode) {
        LagouAutoCode result = codeDao.saveAndFlush(lagouAutoCode);
        return result;
    }

    @Override
    public Integer validateCodeByEmail(String email, String code) {
        LagouAutoCode lagouAutoCode = codeDao.selectCodeByEmail(email);
        //没有返回验证码
        if(lagouAutoCode == null){
            return 3;
        }

        if(code.equals(lagouAutoCode.getCode())){//输入的验证码不正确
            return 1;
        }else if(new Date().compareTo(lagouAutoCode.getExpiretime())!=1){//验证码失效
            return 2;
        }else{
            return 0;
        }

    }
}
